package com.ys.timer;

import com.ys.entity.business.*;
import com.ys.entity.purchase.TcgPurchaseByOrder;
import com.ys.service.business.TpHolidaySetService;
import com.ys.service.business.TpTimeCheckService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import java.util.*;


@Controller
public class TimerTPTimeCheckDaily {
	private static final Logger logger = LoggerFactory.getLogger(TimerTPTimeCheckDaily.class);

	@Autowired
	private TpTimeCheckService tpTimeCheckService;
	@Autowired
	private TpHolidaySetService tpHolidaySetService;


	//每月1号3点执行，统计前一个月的数据
	@Scheduled(cron = "0 0 3 1 * * ")
	//@Scheduled(cron = "5 * * * * * ")
	public void updateTPTimeCheckDaily() {
		try {
			logger.info("***********定时统计时效考核数据开始***********");
			//获取当前时间
			Calendar cal = Calendar.getInstance();
			Date date = new Date();//现在的日期
			cal.setTime(date);
			int year = cal.get(Calendar.YEAR);//获取年
			int month = cal.get(Calendar.MONTH)+1;//获取月（月份从0开始，如果按照中国的习惯，需要加一）
			//这个月
			String thisMonthStr = year + "-" + (month<10 ? "0"+month : month);
			//上个月
			String oldMonthStr = "";
			if(month==1){
				oldMonthStr = (year-1) + "-12";
			}else {
				int oldMonth = month-1;
				oldMonthStr = year + "-" + (oldMonth<10 ? "0"+oldMonth : oldMonth);
			}

			//已经统计的先删除
			tpTimeCheckService.deleteTimeCheck(oldMonthStr);

			//统计组装超时数据
			countAssemble(oldMonthStr,thisMonthStr);

			//统计装箱超时数据
			countPacking(oldMonthStr,thisMonthStr);

			//统计采购超时数据
			countBuy(oldMonthStr,thisMonthStr);
			logger.info("***********定时统计时效考核数据完成***********");
		}catch (Exception e){
			//e.printStackTrace();
			logger.error("定时统计时效考核数据出错：",e);
		}
	}


	//统计采购超时数据
	public void countBuy(String oldMonthStr,String thisMonthStr){
		//List<TcgPurchaseByOrder> buyList = new TcgPurchaseByOrder()
		List<Map<String,Object>> buyList = new TcgPurchaseByOrder()
				//.where("A.State = '已审核' ")
				.addField("P.UpdateTime AS theEndDate")
				.where("CONVERT(VARCHAR ,A.UpdateTime ,120) BETWEEN '"+oldMonthStr+"' AND '"+thisMonthStr+"'")
				.addLeftJoin("TCG_Payment P ON P.OrderID = A.Bid")
				.where("P.UpdateTime IS NOT NULL")
				.queryMap();
		if(buyList!=null && buyList.size()>0){
			for (int i=0;i<buyList.size();i++){
				Date start = (Date) buyList.get(i).get("createTime");// .getCreateTime();
				Date end = (Date) buyList.get(i).get("theEndDate");//.getUpdateTime();

				//获取超时的时间
				int overTime = tpHolidaySetService.checkOverTime(start,end,1)-2;
				//>=2时超时，存入
				if(overTime>0){
					Map<String, Object> tpTimeCheck = new HashMap<>();
					//存入
					tpTimeCheck.put("code",buyList.get(i).get("bid"));
					tpTimeCheck.put("beginTime",(Date) buyList.get(i).get("createTime"));
					tpTimeCheck.put("endTime",(Date) buyList.get(i).get("theEndDate"));
					tpTimeCheck.put("countType",2);
					tpTimeCheck.put("overDay",overTime);
					tpTimeCheck.put("countMonth",oldMonthStr);
					//新增
					tpTimeCheckService.add(tpTimeCheck);
				}
			}
		}
	}

	//统计装箱超时数据
	public void countPacking(String oldMonthStr,String thisMonthStr){
		List<TpFbaBox> packingList = new TpFbaBox()
				.where("A.State =2 ")
				.where("CONVERT(VARCHAR ,A.UpdateTime ,120) BETWEEN '"+oldMonthStr+"' AND '"+thisMonthStr+"'")
				.queryList();
		if(packingList!=null && packingList.size()>0){
			for (int i=0;i<packingList.size();i++){
				Date start = packingList.get(i).getCreateTime();
				Date end = packingList.get(i).getUpdateTime();

				//获取超时的时间
				int overTime = tpHolidaySetService.checkOverTime(start,end,1)-1;
				//>=1时超时，存入
				if(overTime>0){
					Map<String, Object> tpTimeCheck = new HashMap<>();
					//存入
					tpTimeCheck.put("code",packingList.get(i).getBid());
					tpTimeCheck.put("beginTime",packingList.get(i).getCreateTime());
					tpTimeCheck.put("endTime",packingList.get(i).getUpdateTime());
					tpTimeCheck.put("countType",1);
					tpTimeCheck.put("overDay",overTime);
					tpTimeCheck.put("countMonth",oldMonthStr);
					//新增
					tpTimeCheckService.add(tpTimeCheck);
				}
			}
		}
	}


	//统计组装超时数据
	public void countAssemble(String oldMonthStr,String thisMonthStr){
		List<Map<String,Object>> assembleList = new TccPutaway()
				.addField("C.Bid")
				.addField("C.CreateTime AS beginTime")
				.addLeftJoin("( SELECT MAX ( B.CreateTime ) AS CreateTime, B.Bid FROM TCC_ProductAssemble B GROUP BY B.Bid ) C ON A.AssembleID = C.Bid ")
				.where("A.EffectStock = 1")
				.where("CONVERT(VARCHAR ,A.UpdateTime ,120) BETWEEN '"+oldMonthStr+"' AND '"+thisMonthStr+"'")
				.queryMap();
		if(assembleList!=null && assembleList.size()>0){
			for (int i=0;i<assembleList.size();i++){
				Date start = (Date) assembleList.get(i).get("beginTime");
				Date end = (Date) assembleList.get(i).get("updateTime");

				//获取超时的时间
				int overTime = tpHolidaySetService.checkOverTime(start,end,1)-2;
				//>=2时超时，存入
				if(overTime>0){
					Map<String, Object> tpTimeCheck = new HashMap<>();
					//存入
					tpTimeCheck.put("code",assembleList.get(i).get("Bid").toString());
					tpTimeCheck.put("beginTime",(Date) assembleList.get(i).get("beginTime"));
					tpTimeCheck.put("endTime",(Date) assembleList.get(i).get("updateTime"));
					tpTimeCheck.put("countType",0);
					tpTimeCheck.put("overDay",overTime);
					tpTimeCheck.put("countMonth",oldMonthStr);
					//新增
					tpTimeCheckService.add(tpTimeCheck);
				}
			}
		}
	}


}
